package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.fc.cp.api.ViewCsupplierQueryCmd;
import com.jackrain.nea.fc.cp.mapper.ViewCsupplierMapper;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 供应商档案接口-服务开发
 *
 * @author WSL
 * @create 2017/10/20
 */
@Slf4j
@Component
@Service(protocol = "dubbo" , validation = "true" , version = "1.0" , group = "cp")
public class ViewCsupplierQueryCmdImpl extends CommandAdapter implements ViewCsupplierQueryCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        // 常量
        String cpcSuppgradeId = "CP_C_SUPPGRADE_ID";
        String cpcSuppareaId = "CP_C_SUPPAREA_ID";
        String cpcWarehouseId  = "CP_C_WAREHOUSE_ID";
        String modifierId = "MODIFIERID";
        String cpcSuppcateId = "CP_C_SUPPCATE_ID";
        String cpcTityId = "CP_C_TITY_ID";

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCsupplierQueryCmdImpl--param-->{}",param);

        if (null == param || param.size() == 0){
            throw new NDSException("参数格式错误！");
        }
        // Mapper
        ViewCsupplierMapper viewCsupplierMapper = ApplicationContextHandle.getBean(ViewCsupplierMapper.class);

        if ((null == param) || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        // 根据param查询条件 查询【供应商档案】表
        List<HashMap> list = viewCsupplierMapper.selectProvider(param.getJSONArray("IDS").toArray());
        JSONArray resultJson = new JSONArray();
        for (HashMap hashMap: list) {
            // 判断【供应商级别】是否为空 CP_C_SUPPGRADE_ID
            if (null != hashMap.get(cpcSuppgradeId)){
                String eCode = viewCsupplierMapper.selSupdimItemEcode(TypeUtils.castToLong(hashMap.get(cpcSuppgradeId)));
                hashMap.put(cpcSuppgradeId,eCode);
            }
            // 判断【供应商区域】是否为空 CP_C_SUPPAREA_ID
            if (null != hashMap.get(cpcSuppareaId)){
                String eCode = viewCsupplierMapper.selSupdimItemEcode(TypeUtils.castToLong(hashMap.get(cpcSuppareaId)));
                hashMap.put(cpcSuppareaId,eCode);
            }
            // 判断【默认配送仓库】是否为空 CP_C_WAREHOUSE_ID
            if (null != hashMap.get(cpcWarehouseId)){
                String eCode = viewCsupplierMapper.selStoreEcode(TypeUtils.castToLong(hashMap.get(cpcWarehouseId)));
                hashMap.put(cpcWarehouseId,eCode);
            }
            // 判断【修改人】是否为空 MODIFIERID
            if (null != hashMap.get(modifierId)){
                String eName = viewCsupplierMapper.selUserEname(TypeUtils.castToLong(hashMap.get(modifierId)));
                hashMap.put(modifierId,eName);
            }else{
                throw new NDSException("修改人不能为空");
            }
            // 判断【供应商类别】是否为空 CP_C_SUPPCATE_ID
            if (null != hashMap.get(cpcSuppcateId)){
                String eCode = viewCsupplierMapper.selSupdimItemEcode(TypeUtils.castToLong(hashMap.get(cpcSuppcateId)));
                hashMap.put(cpcSuppcateId,eCode);
            }
            // 判断【城市】是否为空 CP_C_TITY_ID
            if (null != hashMap.get(cpcTityId)){
                String eCode = viewCsupplierMapper.selRegionEcode(TypeUtils.castToLong(hashMap.get(cpcTityId)));
                hashMap.put(cpcTityId,eCode);
            }
            // 【启用】翻译
            if ("N".equals(hashMap.get("ISACTIVE"))){
                hashMap.put("ISACTIVE",1);
            }else if ("Y".equals(hashMap.get("ISACTIVE"))){
                hashMap.put("ISACTIVE",0);
            }
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONStringWithDateFormat(new JSONObject(hashMap),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            resultJson.add(jsonObject);
        }
        vh.put("data",resultJson);
        return vh;
    }
}
